package com.gurun.camera.device.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gurun.camera.R;
import com.gurun.camera.R2;
import com.gurun.camera.device.bean.CloudRecordInfo;
import com.gurun.camera.device.util.DateTimeUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    List<CloudRecordInfo> cloudRecordInfos;

    public RecordAdapter(Context context, List<CloudRecordInfo> cloudRecordInfos){
        this.context = context;
        this.cloudRecordInfos = cloudRecordInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.item_record, parent, false);
        return new RecordViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        CloudRecordInfo cloudRecordInfo = cloudRecordInfos.get(position);
        if (holder instanceof RecordViewHolder){
            ((RecordViewHolder) holder).time.setText(DateTimeUtil.getTimeString(cloudRecordInfo.getStartTime(), DateTimeUtil.PATTERN_YMD_HMS_1)
                    + " - " + DateTimeUtil.getTimeString(cloudRecordInfo.getStartTime() + cloudRecordInfo.getTimeLen(), DateTimeUtil.PATTERN_YMD_HMS_1));
            ((RecordViewHolder) holder).layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null){
                        onItemClickListener.onItemClick(position);
                    }
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        if (cloudRecordInfos != null){
            return cloudRecordInfos.size();
        }else {
            return 0;
        }
    }

    public static class RecordViewHolder extends RecyclerView.ViewHolder {

        @BindView(R2.id.record_time)
        TextView time;
        @BindView(R2.id.layout)
        LinearLayout layout;

        public RecordViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
